<template>
  <div>
    <div id="top">
      <el-menu
          class="el-menu-demo"
          mode="horizontal"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b">
        <el-menu-item index="1">{{ nickName }}</el-menu-item>
        <el-menu-item index="1">
          <a href="#/">继续购物</a>
        </el-menu-item>
        <el-menu-item index="1">
          <el-badge :value="0" class="item">
            <a href="#/showCart">我的购物车</a>
          </el-badge>
        </el-menu-item>
        <el-menu-item index="1">
          <a href="javascript:;" @click="logout">注销</a>
        </el-menu-item>
      </el-menu>
    </div>
    <!-- 加载当前用户的地址区域  -->
    <div id="address">
      <h3>我的地址：</h3>
      <span v-for="a in addresses">
        &nbsp;
        <input type="radio" name="address" :value="a.id" v-model="address"/>
        姓名:{{ a.contact }},地址:{{ a.addressDesc }},手机:{{ a.tel }}
      <br/>
      </span>
    </div>
    <div id="order">
      <el-table
          :data="goods"
          style="width: 100%">
        <el-table-column
            prop="goodsCode"
            label="编号"
            width="180">
        </el-table-column>
        <el-table-column
            prop="goodsName"
            label="名称"
            width="180">
        </el-table-column>
        <el-table-column
            prop="goodsPrice"
            label="价格">
        </el-table-column>
        <el-table-column
            prop="provider.data.proName"
            label="供应商">
        </el-table-column>
        <el-table-column
            label="数量"
            width="100"
            prop="cartNum">
        </el-table-column>
        <el-table-column
            prop="total"
            label="价格">
        </el-table-column>
      </el-table>
      <div align="right">
        <label style="font-size: 4em;">总价：¥<b>{{ total }}</b>元</label>
        <el-button type="primary" plain @click="pay">付款</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "Order",
  data() {
    return {
      nickName: window.localStorage.getItem('nickName'),
      addresses: [],
      address: '',
      goods: [],
      total: 0
    }
  },
  created() {
    this.loadMyAddress()
    this.loadMyCart()
  },
  methods: {
    loadMyAddress() {
      let _this = this
      axios.get('/api/smbms-address/address', {
        headers: {
          token: window.localStorage.getItem('token')
        }
      }).then((response) => {
        console.log(response.data)
        _this.addresses = response.data.data
        _this.address = response.data.errorMsg
      }).catch(() => {
        _this.$router.push('login')
      })
    },
    loadMyCart() {
      let _this = this;
      axios.get('/api/smbms-goods/goods/show', {
        headers: {
          token: window.localStorage.getItem('token')
        }
      }).then((response) => {
        _this.goods = response.data.data
        _this.total = response.data.errorMsg
      }).catch(() => {
        _this.$router.push('login')
      })
    },
    pay() {
      let _this = this
      let body = ''  // 商品备注
      let array = this.goods;
      for (let i = 0; i < array.length; i++) {
        body += array[i].goodsName + ","
      }
      // 订单备注
      body = body.substr(0, body.length - 1)
      // 订单总价
      let total_amount = this.total
      // 订单编号 java自动生成
      // 订单名称
      let date = new Date()
      let subject = this.nickName + '' + (date.getMonth() + 1) + "月" + date.getDate() + '日的订单'
      // 收货人id
      let aid = this.address
      // 订单商品详情(bid和对应的num)
      // 1,2.2,1.3,2
      let order = '';
      for (let i = 0; i < this.goods.length; i++) {
        let g = this.goods[i];
        order += g.id + '=' + g.cartNum + ','
      }
      order = order.substr(0, order.length - 1)
      console.log(body)
      console.log(total_amount)
      console.log(subject)
      console.log(aid)
      console.log(order)
      let param = '?aid=' + aid + '&order=' + order + '&body=' + body + '&total_amount=' + total_amount + '&subject=' + subject
      location.href = 'http://localhost:8029/alipay/pay' + param;
    }
  }
}
</script>

<style scoped>

</style>